/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.qiujuer.genius.ui.drawable;

import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.ColorFilter;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.media.image.common.PixelFormat;

import net.qiujuer.genius.ui.Ui;
import net.qiujuer.genius.ui.widget.AbsSeekBar;

/**
 * A drawable that changes it's Paint and color depending on the Drawable State
 * For SeekBarDrawable {@link SeekBarDrawable}
 *
 * @Hide
 */
public abstract class SeekBarStateDrawable extends ShapeElement implements Component.DrawTask {
    private final Paint mPaint;
    private int mAlpha = 255;

    private int mTrackStateList;
    private int mTrackColor;

    private int mScrubberStateList;
    private int mScrubberColor;

    private int mThumbStateList;
    private int mThumbColor;

    private int mCurTrackColor;
    private int mCurScrubberColor;
    private int mCurThumbColor;

    Callback mCallback;

    public static interface Callback {
        public void invalidate();
    }

    public SeekBarStateDrawable(AbsSeekBar absSeekBar, int trackStateList, int scrubberStateList, int thumbStateList) {
        super();
        mPaint = new Paint();
        setTrackColor(trackStateList);
        setScrubberColor(scrubberStateList);
        setThumbColor(thumbStateList);
    }

    protected SeekBarStateDrawable() {
        mPaint = new Paint();
    }

    @Override
    public boolean isStateful() {
        return super.isStateful();
    }


    @Override
    public void onDraw(Component component, Canvas canvas) {
        System.out.println("========SeekBarDrawable============06");
        draw(canvas, mPaint, mCurTrackColor, mCurScrubberColor, mCurThumbColor);
    }

    public boolean setState(int[] stateSet) {
        if (stateSet == null)
            return false;
        boolean changed = true;
        changed = updateTintColor(stateSet) || changed;
        return changed;
    }

    public int getOpacity() {
        return PixelFormat.ARGB_8888.getValue();
    }

    @Override
    public int getAlpha() {
        return mAlpha;
    }

    @Override
    public void setAlpha(int alpha) {
        mAlpha = alpha;
        updateCurColor();
        invalidateSelf();
    }

    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }

    /**
     * Set the Track ColorStateList
     *
     * @param stateList ColorStateList
     */
    public void setTrackColor(int stateList) {
        mTrackStateList = stateList;
        mTrackColor = mTrackStateList;
        if (mAlpha < 255) {
            mCurTrackColor = Ui.modulateColorAlpha(mTrackColor, mAlpha);
        } else {
            mCurTrackColor = mTrackColor;
        }
    }

    /**
     * Get the Track ColorStateList
     *
     * @return int int
     */
    public int getTrackColor() {
        return mTrackStateList;
    }

    /**
     * Set the Scrubber ColorStateList
     *
     * @param stateList ColorStateList
     */
    public void setScrubberColor(int stateList) {
        mScrubberStateList = stateList;
        mScrubberColor = mScrubberStateList;

        if (mAlpha < 255) {
            mCurScrubberColor = Ui.modulateColorAlpha(mScrubberColor, mAlpha);
        } else {
            mCurScrubberColor = mScrubberColor;
        }
    }

    /**
     * Get the Scrubber ColorStateList
     *
     * @return int int
     */
    public int getScrubberColor() {
        return mScrubberStateList;
    }


    /**
     * Set the Thumb ColorStateList
     *
     * @param stateList ColorStateList
     */
    public void setThumbColor(int stateList) {
        mThumbStateList = stateList;
        mThumbColor = mThumbStateList;

        if (mAlpha < 255) {
            mCurThumbColor = Ui.modulateColorAlpha(mThumbColor, mAlpha);
        } else {
            mCurThumbColor = mThumbColor;
        }
    }


    /**
     * Get the Thumb ColorStateList
     *
     * @return int int
     */
    public int getThumbColor() {
        return mThumbStateList;
    }


    /**
     * Get the Tint ColorStateList
     *
     * @return mTintStateList
     */
    public int[] getColor() {
        return new int[]{mTrackStateList, mScrubberStateList, mThumbStateList};
    }

    /**
     * Get The CurrentColor
     *
     * @return mCurrentColor
     */
    public int[] getCurrentColor() {
        return new int[]{mCurTrackColor, mCurScrubberColor, mCurThumbColor};
    }


    /**
     * Subclasses should implement this method to do the actual drawing
     * @param canvas The current {@link Canvas} to draw into
     * @param paint The {@link Paint} the Paint object that defines with the current
     * @param trackColor trackColor
     * @param scrubberColor scrubberColor
     * @param thumbColor thumbColor
     */
    public abstract void draw(Canvas canvas, Paint paint, int trackColor, int scrubberColor, int thumbColor);


    /**
     * Update tint color
     *
     * @param state tint stats
     * @return is changed
     */
    private boolean updateTintColor(int[] state) {
        /*final int trackColor = mTrackStateList.getColorForState(state, mTrackColor);
        final int scrubberColor = mScrubberStateList.getColorForState(state, mScrubberColor);
        final int thumbColor = mThumbStateList.getColorForState(state, mThumbColor);*/
//        final int trackColor = mTrackColor;
//        final int scrubberColor = mScrubberColor;
//        final int thumbColor = mThumbColor;
//        if (trackColor != mTrackColor || scrubberColor != mScrubberColor || thumbColor != mThumbColor) {
//            mTrackColor = trackColor;
//            mScrubberColor = scrubberColor;
//            mThumbColor = thumbColor;
//            updateCurColor();
//            //invalidateSelf();
//            return true;
//        }

        return false;
    }

    private void updateCurColor() {
        if (mAlpha < 255) {
            mCurTrackColor = Ui.modulateColorAlpha(mTrackColor, mAlpha);
            mCurScrubberColor = Ui.modulateColorAlpha(mScrubberColor, mAlpha);
            mCurThumbColor = Ui.modulateColorAlpha(mThumbColor, mAlpha);
        } else {
            mCurTrackColor = mTrackColor;
            mCurScrubberColor = mScrubberColor;
            mCurThumbColor = mThumbColor;
        }
    }

    /**
     * 設置回調
     *
     * @param mCallback 回調
     */
    public void setMCallback(Callback mCallback) {
        this.mCallback = mCallback;
    }

    public void invalidateSelf() {
        mCallback.invalidate();
    }
}
